package com.zz.qimo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.zz.qimo.sqlite.DatabaseHelper;

public class LoginActivity extends AppCompatActivity {
    private EditText usernameEditText, passwordEditText;
    private Button loginButton, registerButton, changePasswordButton;

    private DatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        // 初始化视图和数据库帮助类
        usernameEditText = findViewById(R.id.editTextUsername);
        passwordEditText = findViewById(R.id.editTextPassword);
        loginButton = findViewById(R.id.buttonLogin);
        registerButton = findViewById(R.id.buttonRegister);
        changePasswordButton = findViewById(R.id.buttonChangePassword);

        dbHelper = new DatabaseHelper(this);

        // 登录按钮点击事件
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = usernameEditText.getText().toString().trim();
                String password = passwordEditText.getText().toString().trim();

                // 检查用户名和密码是否为空
                if (username.isEmpty() || password.isEmpty()) {
                    Toast.makeText(LoginActivity.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                // 验证用户
                if (isValidUser(username, password)) {
                    // 登录成功后的操作，比如跳转到主页
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    finish();
                } else {
                    // 登录失败，显示错误消息
                    Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                }
            }
        });

        // 注册按钮点击事件
        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到注册界面
                startActivity(new Intent(LoginActivity.this, SignUpActivity.class));
            }
        });

        // 修改密码按钮点击事件
        changePasswordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到修改密码界面
                startActivity(new Intent(LoginActivity.this, ChangePasswordActivity.class));
            }
        });
    }

    private boolean isValidUser(String username, String password) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();

        try {
            Cursor cursor = db.query(
                    "Users",
                    new String[]{"id"},
                    "username = ? AND password = ?",
                    new String[]{username, password},
                    null, null, null
            );

            boolean isValid = cursor.getCount() > 0;
            cursor.close();
            return isValid;
        } catch (Exception e) {
            // 处理数据库异常
            e.printStackTrace();
            Toast.makeText(LoginActivity.this, "登录失败，请重试", Toast.LENGTH_SHORT).show();
            return false;
        } finally {
            db.close();
        }
    }
}
